package com.swingson.other;

import com.swingson.SwingsonGui;
import com.swingson.exceptions.SwingsonException;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.imageio.ImageIO;
import javax.swing.Icon;
import javax.swing.ImageIcon;

/**
 * @author sad
 */
public class IconLoader {

    private Icon loadFromClassPath(String path, SwingsonGui gui) {
        Class controllerClass = gui.getControllerComponent().getClass();
        try (InputStream stream = controllerClass.getResourceAsStream(path)) {
            if (stream == null) {
                throw new SwingsonException(String.format("Cannot load icon %s from classpath %s", path, controllerClass.getPackage().getName()));
            }
            try {
                BufferedImage image = ImageIO.read(stream);
                return new ImageIcon(image);
            } catch (IOException ex) {
                throw new SwingsonException(String.format("Cannot parse resource %s at %s as image", path, controllerClass.getPackage().getName()));
            }
        } catch (IOException ex) {
            throw new SwingsonException(String.format("error while reading classpath icon from resource stream %s", path));
        }
    }

    private Icon loadFromFile(String path, SwingsonGui gui) {
        File file = new File(path);
        if (!file.exists()) {
            return null;
        }
        try (InputStream stream = new FileInputStream(file)) {
            if (stream == null) {
                throw new SwingsonException(String.format("Cannot load icon %s from file", path));
            }
            try {
                BufferedImage image = ImageIO.read(stream);
                return new ImageIcon(image);
            } catch (IOException ex) {
                throw new SwingsonException(String.format("Cannot parse resouce from file %s as image", path));
            }
        } catch (IOException ex) {
            throw new SwingsonException(String.format("error while reading icon from resource stream %s", path));
        }
    }

    public Icon loadImage(String path, SwingsonGui gui) {
        if (path.startsWith("classpath:")) {
            return loadFromClassPath(path.substring("classpath:".length()), gui);
        } else if (path.startsWith("file:")) {
            return loadFromFile(path.substring("file:".length()), gui);
        } else {
            Icon icon = null;
            try {
                icon = loadFromFile(path, gui);
                if (icon != null) {
                    return icon;
                }
            } catch (Exception ex) {
                //ignore
            }
            try {
                icon = loadFromClassPath(path, gui);
                if (icon != null) {
                    return icon;
                }
            } catch (Exception ex) {
                //ignore
            }

            throw new SwingsonException("cannot load image from " + path);
        }
    }

}
